﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ChatUI
{
    class ChatBubble
    {
        public ChatBubble(Panel panel)
        {
            /*if (panel.Controls.Count != 0) throw new Exception("指定Panel控件不为空!");*/
            ChatPlace = panel;
            // BubbleFont = font;
            MsgMaxLength = panel.Width - (6 * 4 + 35 * 2);                       // 其中, 四个6为 图片与(容器以及消息文本框)的距离 ,两个35为两侧图片的大小.
        }

        readonly Panel ChatPlace;
        //readonly Font BubbleFont;

        int NowY = 7;

        readonly int MsgMaxLength;

        public enum MsgPlace
        {
            Left,
            Right
        }              // 气泡创建的位置

        public void AddMsg(Image Photo, string Text, MsgPlace Place, string Name)
        {
            if (ChatPlace.Controls.Count != 0) NowY = ChatPlace.Controls[ChatPlace.Controls.Count - 2].Location.Y + ChatPlace.Controls[ChatPlace.Controls.Count - 1].Height + 7;

            PictureBox photo = new PictureBox();                // 头像
            Label nickname = new Label();                       // 昵称
            Label msg = new Label();                            // 消息内容

            photo.Size = new Size(40, 40);
            photo.SizeMode = PictureBoxSizeMode.StretchImage;
            photo.Image = Photo;

            nickname.AutoSize = true;
            nickname.MaximumSize = new Size(0, 0);
            msg.Font = new Font("幼圆", 11, FontStyle.Bold);
            msg.ForeColor = Color.IndianRed;
            //nickname.Font = BubbleFont;
            nickname.Text = Name;

            msg.AutoSize = true;
            msg.Font = new Font("幼圆", 13, FontStyle.Bold);
            msg.ForeColor = Color.White;
            msg.BackColor = Color.IndianRed;
            msg.MaximumSize = new Size(MsgMaxLength, 0);
            msg.Text = Text;
            msg.BorderStyle = BorderStyle.FixedSingle;

            ChatPlace.Controls.Add(nickname);
            ChatPlace.Controls.Add(photo);
            ChatPlace.Controls.Add(msg);

            if (Place == MsgPlace.Left)
            {
                photo.Location = new Point(7, NowY);
                nickname.Location = new Point(photo.Location.X + photo.Width + 6, NowY);
                msg.Location = new Point(nickname.Location.X, NowY + nickname.Size.Height);
            }
            else
            {
                photo.Location = new Point(ChatPlace.Width - 7 - 35 - 20, NowY);
                nickname.Location = new Point(photo.Location.X - 7 - nickname.Width - 20, NowY);
                msg.Location = new Point(photo.Location.X - 7 - msg.Width - 20, NowY + nickname.Size.Height);

            }
        }
    }

}
